package cn.tedu.powerfulfitness.controller;

import cn.tedu.powerfulfitness.pojo.dto.UserAddNewDTO;
import cn.tedu.powerfulfitness.pojo.dto.UserUpdateDTO;
import cn.tedu.powerfulfitness.pojo.vo.UserListItemVO;
import cn.tedu.powerfulfitness.pojo.vo.UserStandardVO;
import cn.tedu.powerfulfitness.service.IUserService;
import cn.tedu.powerfulfitness.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理相册相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@RestController
@RequestMapping("/users")
@Api(tags = "01. 用户管理模块")
@Validated
public class UserController {

    @Autowired
    private IUserService userService;

    // http://localhost:9080/album/add-new?name=TestName001&description=TestDescription001&sort=199
    @PostMapping("/add-new")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid UserAddNewDTO userAddNewDTO) {
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "删除用户失败，ID值无效！")
                             @PathVariable Integer id) {
        userService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改用户详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "修改用户详情失败，ID值无效！")
                                     @PathVariable Integer id, @Valid UserUpdateDTO userUpdateDTO) {
        userService.updateInfoById(id, userUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询用户详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "获取用户详情失败，ID值无效！")
                             @PathVariable Integer id) {
        UserStandardVO queryResult = userService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9080/albums
    @GetMapping("")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }
}
